package com.tiyende.credit.loop.ui.activity

import android.annotation.SuppressLint
import android.os.CountDownTimer
import android.view.View
import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.common.CommonActivity
import com.tiyende.credit.loop.databinding.TiyendeActivityLimitReviewingBinding
import com.tiyende.credit.loop.event.TiyendeEventData
import com.tiyende.credit.loop.event.TiyendeEventPointType
import com.tiyende.credit.loop.ui.contract.IViewContract
import com.tiyende.credit.loop.ui.presenter.TiyendeLimitPresenter
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/12/16 13:41
 * @Annotation：额度审核中
 */
class TiyendeLimitReviewingActivity: CommonActivity(),IViewContract.ITiyendeLimitReviewingView {
    lateinit var binding: TiyendeActivityLimitReviewingBinding
    private var presenter: TiyendeLimitPresenter? = null
    private var countdownTimer: MyCountDownTimer? = null
    //等待时长
    private var countdown = 15

    override fun getTiyendeRootView(): View? {
        binding = TiyendeActivityLimitReviewingBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initTiyendeView() {
        setTiyendeTitleBarVisibility(View.VISIBLE)
        setTiyendeTextTitleVisibility(View.GONE)
        setTiyendeBackBtnVisibility(View.INVISIBLE)
        setTiyendeRightBtnRes(R.mipmap.tiyende_customer_white)
    }

    override fun initTiyendeListener() {
        rootViewBinding.rootCustomer.setOnClickListener {
            showCustomerDialog("Limit Reviewing Page")
            addEventPointRequestAction(TiyendeEventPointType.CLICK.TIYENDE_CLICK_CUSTOMER,"Limit Reviewing Page")
        }
    }

    override fun loadTiyendeData() {
        if (presenter == null){
            presenter = TiyendeLimitPresenter(this)
        }
        presenter?.queryTiyendeCreditStatusAction()
    }

    /**
     * 额度审核中
     */
    @SuppressLint("SetTextI18n")
    override fun showTiyendeLimitReviewingViews() {
        //启动倒计时
        startCountdownTimer()
    }

    /**
     * 额度审核成功
     */
    override fun showTiyendeLimitSuccessViews(data: JSONObject) {
        onIntent(TiyendeLimitCompleteActivity::class.java)
        if (countdownTimer != null){
            countdownTimer?.cancel()
        }
        finish()
    }

    /**
     * 额度审核失败
     */
    override fun showTiyendeLimitFailedViews() {
        onIntent(TiyendeLimitRejectedActivity::class.java)
        if (countdownTimer != null){
            countdownTimer?.cancel()
        }
        finish()
    }

    /**
     * 申贷
     */
    override fun showTiyendeLimitApplyPage() {
        TiyendeLoanApplyActivity.start(this)
        finish()
    }

    override fun showTiyendeLimitPermissionPage() {
        onIntent(TiyendePermissionListActivity::class.java)
        finish()
    }

    /**
     * 开启倒计时
     */
    private fun startCountdownTimer(){
        if (countdownTimer == null){
            countdownTimer = MyCountDownTimer(this,countdown.toLong() * 1000,1000)
        }
        countdownTimer?.start()
    }

    /**
     * 显示状态
     */
    private fun showLimitCountdownText(){
        if (countdown > 10){
            binding.imageLimitReViewStep1.setImageResource(R.mipmap.tiyende_loan_apply_wait_step2_icon)
            binding.imageLimitReViewStep2.setImageResource(R.mipmap.tiyende_loan_apply_wait_step3_icon)
            binding.imageLimitReViewStep3.setImageResource(R.mipmap.tiyende_loan_apply_wait_step3_icon)
            return
        }

        if (countdown > 5){
            binding.imageLimitReViewStep1.setImageResource(R.mipmap.tiyende_loan_apply_wait_step1_icon)
            binding.imageLimitReViewStep2.setImageResource(R.mipmap.tiyende_loan_apply_wait_step2_icon)
            binding.imageLimitReViewStep3.setImageResource(R.mipmap.tiyende_loan_apply_wait_step3_icon)
            return
        }
        binding.imageLimitReViewStep1.setImageResource(R.mipmap.tiyende_loan_apply_wait_step1_icon)
        binding.imageLimitReViewStep2.setImageResource(R.mipmap.tiyende_loan_apply_wait_step1_icon)

        if (countdown >= 2){
            binding.imageLimitReViewStep3.setImageResource(R.mipmap.tiyende_loan_apply_wait_step2_icon)
            return
        }
        binding.imageLimitReViewStep3.setImageResource(R.mipmap.tiyende_loan_apply_wait_step1_icon)
        showTiyendeLoading()
    }

    private fun showLimitCountdownComplete(){
        publishEventData(TiyendeEventData(TiyendeEventData.TYPE.TIYENDE_PROCESS_REFRESH))
    }

    class MyCountDownTimer(private val act: TiyendeLimitReviewingActivity, millisInFuture: Long, countDownInterval: Long): CountDownTimer(millisInFuture, countDownInterval) {
        override fun onTick(millisUntilFinished: Long) {
            // 这里每隔一段时间（定时器间隔）会被调用
            // 可以在这里更新UI，显示剩余时间
            act.countdown = (millisUntilFinished / 1000).toInt()
            act.showLimitCountdownText()
        }
        override fun onFinish() {
            // 倒计时完成时被调用
            act.showLimitCountdownComplete()
        }
    }

    override fun onTiyendePublishEvent(event: TiyendeEventData) {
        super.onTiyendePublishEvent(event)
        if (event.eventType == TiyendeEventData.TYPE.TIYENDE_PROCESS_REFRESHED){
            presenter?.queryTiyendeCreditStatusAction()
        }
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(TiyendeEventPointType.PAGE.TIYENDE_PAGE_CREDIT_REVIEWING_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(TiyendeEventPointType.PAGE.TIYENDE_PAGE_CREDIT_REVIEWING_END)
        if (countdownTimer != null){
            countdownTimer?.cancel()
            countdownTimer = null
        }
    }
}